package cn.appsys.controller.dev;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.appsys.pojo.DevUser;
import cn.appsys.service.dev.user.DevUserService;
import cn.appsys.tools.Constants;

/**
 * 前端开发，用户控制类
 * @author 威威
 *
 */
@Controller
@RequestMapping("/dev/login")
public class DevLoginController {
	
	//注入Service层对象
	@Resource
	private DevUserService devUserService;
	
	
	/**
	 * 进入登录页面
	 * @return
	 */
	@RequestMapping(value="/tologin",method=RequestMethod.GET)
	public String tologin() {
		return "devlogin";
	}
	
	
	/**
	 * 验证登录
	 * @return
	 */
	@RequestMapping(value="/dologin",method=RequestMethod.POST)
	public String dologin(HttpSession session,HttpServletRequest request,
				@RequestParam(value="devCode")String devCode,
				@RequestParam(value="devPassword")String devPassword) {
		
		//调用Service层方法
		DevUser devUser = devUserService.queryDevUser(devCode, devPassword);
		if (devUser != null) {//如果不等于空就登录成功
			session.setAttribute(Constants.DEV_USER, devUser);
			return "developer/main";
		}else{
			request.setAttribute("error", "用户或密码错误");
			return "dev/login";
		}
	}
	
	/**
	 * 注销登录
	 * @return
	 */
	@RequestMapping(value="/logout",method=RequestMethod.GET)
	public String logout(HttpSession session) {
		session.removeAttribute(Constants.DEV_USER);
		return "redirect:/dev/login";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}
